Public Class TransferDialog
    Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    Protected WithEvents repTrans As System.Web.UI.WebControls.Repeater
    Protected WithEvents btnReset As System.Web.UI.WebControls.Button
    Protected WithEvents btnSave As System.Web.UI.WebControls.Button

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

#Region "   Properties"


    Protected ReadOnly Property PageBO() As CProductionStatus
        Get
            If IsNothing(Session(ProductionStatusUpdate.PRODUCTION_UPDATE_PAGEBO_SESSION_NAME)) Then
                Throw New ApplicationException("Session expried. Please re-login")
            End If
            Dim idx As Integer
            idx = CInt(Request.QueryString("idx"))
            Return CType(CType(Session(ProductionStatusUpdate.PRODUCTION_UPDATE_PAGEBO_SESSION_NAME), ArrayList)(idx), CProductionStatus)
        End Get
    End Property

    Protected Property OrderBO() As COrder
        Get
            If IsNothing(Session(OrderInput.ORDER_INPUT_PAGEBO_SESSION_NAME)) Then
                Throw New ApplicationException("Session expried. Please re-login")
            End If
            Return CType(Session(OrderInput.ORDER_INPUT_PAGEBO_SESSION_NAME), COrder)
        End Get
        Set(ByVal Value As COrder)
            Session(OrderInput.ORDER_INPUT_PAGEBO_SESSION_NAME) = Value
        End Set
    End Property

    Protected ReadOnly Property BOOrderItemID() As Integer
        Get
            Dim oiID As Integer
            oiID = CInt(Request.QueryString("oiID"))

            Return oiID
        End Get
    End Property


#End Region

    Private Sub BindData()
        'build Transfer data table for displaying
        Dim dtTrans As New DataTable
        dtTrans.Columns.Add("ID")
        dtTrans.Columns.Add("PONumber")
        dtTrans.Columns.Add("Quantity")
        dtTrans.Columns.Add("LastQuantity")
        dtTrans.Columns.Add("QuantityOrdered")

        Dim i, j As Integer
        For i = 0 To PageBO.Transfers.Count - 1
            Dim row As DataRow = dtTrans.NewRow
            row(0) = PageBO.Transfer(i).ID
            row(1) = PageBO.Transfer(i).PurchaseOrderNumber
            row(2) = IIf(PageBO.Transfer(i).Quantity >= 0, PageBO.Transfer(i).Quantity, "")
            row(3) = "N/A"
            row(4) = GetQuantityOrdered(PageBO.Transfer(i).PurchaseOrderID)
            dtTrans.Rows.Add(row)
        Next

        Dim objLastWeek As CProductionStatus

        Try
            objLastWeek = PageBO.LastWeek
        Catch ex As Exception

        End Try

        If Not IsNothing(objLastWeek) Then
            For j = 0 To objLastWeek.Transfers.Count - 1
                dtTrans.Rows(j)(3) = objLastWeek.Transfer(j).Quantity
            Next
        End If

        Me.repTrans.DataSource = dtTrans
        Me.repTrans.DataBind()
    End Sub

    Private Function GetPOOrderItemID(ByVal orderID As Integer) As Integer
        Dim query As String
        query = "SELECT OI_ID_PK FROM ORDER_ITEM WHERE ORDER_ID_FK = {0} AND USE_FROM_BO_ITEM = {1}"
        query = String.Format(query, orderID, Me.BOOrderItemID)

        Dim sqlCmd As New SqlClient.SqlCommand(query)
        Dim obj As Object = DataHelper.ExecuteScalar(sqlCmd)

        Return CInt(obj)
    End Function

    Private Function GetQuantityOrdered(ByVal orderID As Integer) As Integer

        Dim orderItemID As Integer = GetPOOrderItemID(orderID)
        If (orderItemID <= 0) Then
            Return 0
        End If

        Dim orderItem As New COrderItem
        orderItem.Load(orderItemID)
        If (orderItem.AdjustedQuantity > 0) Then
            Return orderItem.AdjustedQuantity
        Else
            Return orderItem.Quantity
        End If
    End Function

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If Not Me.IsPostBack Then
            Me.BindData()
        End If
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        'transfer item
        Dim repItem As RepeaterItem
        Dim i As Integer
        For i = 0 To repTrans.Items.Count - 1
            repItem = repTrans.Items(i)
            Dim qty As Integer
            qty = CInt(CType(repItem.FindControl("txtQuantity"), TextBox).Text)
            PageBO.Transfer(i).Quantity = qty
        Next

        Response.Write("<script>window.parent.close();</script")
    End Sub

    Private Sub btnReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReset.Click
        Me.BindData()
    End Sub
End Class
